import 'package:lib_ui/common/theme/app_theme_colors.dart';
import 'package:flutter/material.dart';
import 'package:lib_core/lib_core.dart';
import 'package:lib_ui/common/theme/theme_color_cst.dart';
import 'package:lib_ui/common/theme/theme_text_cst.dart';

class ThemeController extends GetxController {
  final isDarkMode = false.obs;

  ThemeData get currentTheme => isDarkMode.value ? _darkTheme : _lightTheme;

  final _lightTheme = ThemeData(
    // 应用的整体亮度
    brightness: Brightness.light,
    // 主题色
    primaryColor: ThemeColorCst.primaryColorLight,
    // 主颜色的浅变体
    primaryColorLight: ThemeColorCst.primaryColorLightLight,
    // 主颜色的深变体
    primaryColorDark: ThemeColorCst.primaryColorLightDark,
    // Scaffold 背景色
    scaffoldBackgroundColor: ThemeColorCst.scaffoldBackgroundColorLight,
    // 分割线颜色
    dividerColor: ThemeColorCst.dividerColorLight,
    // 提示颜色
    hintColor: ThemeColorCst.hintColorLight,
    // 聚焦颜色
    focusColor: ThemeColorCst.focusColorLight,
    // appBar主题
    appBarTheme: const AppBarTheme(
      iconTheme: IconThemeData(color: ThemeColorCst.iconColorLight),
      backgroundColor: ThemeColorCst.scaffoldBackgroundColorLight,
      titleTextStyle:
          TextStyle(color: ThemeColorCst.textColorLight, fontSize: ThemeTextCst.appBarTextSize, fontWeight: ThemeTextCst.appBarTextFontWeight),
    ),
    // 全局文本样式（包含多种预定义的文本样式）
    textTheme: const TextTheme(
      // AppBar 标题
      titleLarge:
          TextStyle(color: ThemeColorCst.appBarColorLight, fontSize: ThemeTextCst.appBarTextSize, fontWeight: ThemeTextCst.appBarTextFontWeight),
      // 正文
      bodyLarge: TextStyle(color: ThemeColorCst.textColorLight, fontSize: ThemeTextCst.textBigSize, fontWeight: ThemeTextCst.textBigFontWeight),
      bodyMedium: TextStyle(color: ThemeColorCst.textColorLight, fontSize: ThemeTextCst.textMediumSize, fontWeight: ThemeTextCst.textMediumFontWeight),
      bodySmall: TextStyle(color: ThemeColorCst.textColorLight, fontSize: ThemeTextCst.textSmallSize, fontWeight: ThemeTextCst.textSmallFontWeight),
      // 按钮文本
      labelLarge: TextStyle(color: ThemeColorCst.textColorLight, fontSize: ThemeTextCst.labelBigSize, fontWeight: ThemeTextCst.labelBigFontWeight),
      labelMedium: TextStyle(color: ThemeColorCst.textColorLight, fontSize: ThemeTextCst.labelMediumSize, fontWeight: ThemeTextCst.labelMediumFontWeight),
      labelSmall: TextStyle(color: ThemeColorCst.textColorLight, fontSize: ThemeTextCst.labelSmallSize, fontWeight: ThemeTextCst.labelSmallFontWeight),
    ),
    textButtonTheme: const TextButtonThemeData(
      style: ButtonStyle(
        textStyle: WidgetStatePropertyAll(TextStyle(fontSize: ThemeTextCst.textMediumSize, fontWeight: ThemeTextCst.textMediumFontWeight)),
        backgroundColor: WidgetStatePropertyAll(ThemeColorCst.textButtonBackgroundColorLight),
        foregroundColor: WidgetStatePropertyAll(ThemeColorCst.textButtonForegroundColorLight),
      ),
    ),
    iconTheme: const IconThemeData(
      color: ThemeColorCst.iconColorLight,
    ),
    extensions: <ThemeExtension<dynamic>>[
      AppThemeColors(
        widgetBackgroundColor: Colors.grey,
        appBarLeadingColor: Colors.black,
      ),
    ],
  );

  final _darkTheme = ThemeData(
    // 应用的整体亮度
    brightness: Brightness.dark,
    // 主题色
    primaryColor: ThemeColorCst.primaryColorDark,
    // 主颜色的浅变体
    primaryColorLight: ThemeColorCst.primaryColorLightDark,
    // 主颜色的深变体
    primaryColorDark: ThemeColorCst.primaryColorDarkDark,
    // Scaffold 背景色
    scaffoldBackgroundColor: ThemeColorCst.scaffoldBackgroundColorDark,
    // 分割线颜色
    dividerColor: ThemeColorCst.dividerColorDark,
    // 提示颜色
    hintColor: ThemeColorCst.hintColorDark,
    // 聚焦颜色
    focusColor: ThemeColorCst.focusColorDark,
    // appBar主题
    appBarTheme: const AppBarTheme(
      iconTheme: IconThemeData(color: ThemeColorCst.iconColorDark),
      backgroundColor: ThemeColorCst.scaffoldBackgroundColorDark,
      titleTextStyle:
          TextStyle(color: ThemeColorCst.textColorDark, fontSize: ThemeTextCst.appBarTextSize, fontWeight: ThemeTextCst.appBarTextFontWeight),
    ),
    // 全局文本样式（包含多种预定义的文本样式）
    textTheme: const TextTheme(
      // AppBar 标题
      titleLarge:
          TextStyle(color: ThemeColorCst.appBarColorDark, fontSize: ThemeTextCst.appBarTextSize, fontWeight: ThemeTextCst.appBarTextFontWeight),
      // 正文
      bodyLarge: TextStyle(color: ThemeColorCst.textColorDark, fontSize: ThemeTextCst.textBigSize, fontWeight: ThemeTextCst.textBigFontWeight),
      bodyMedium: TextStyle(color: ThemeColorCst.textColorDark, fontSize: ThemeTextCst.textMediumSize, fontWeight: ThemeTextCst.textMediumFontWeight),
      bodySmall: TextStyle(color: ThemeColorCst.textColorDark, fontSize: ThemeTextCst.textSmallSize, fontWeight: ThemeTextCst.textSmallFontWeight),
      // 按钮文本
      labelLarge: TextStyle(color: ThemeColorCst.textColorDark, fontSize: ThemeTextCst.labelBigSize, fontWeight: ThemeTextCst.labelBigFontWeight),
      labelMedium: TextStyle(color: ThemeColorCst.textColorDark, fontSize: ThemeTextCst.labelMediumSize, fontWeight: ThemeTextCst.labelMediumFontWeight),
      labelSmall: TextStyle(color: ThemeColorCst.textColorDark, fontSize: ThemeTextCst.labelSmallSize, fontWeight: ThemeTextCst.labelSmallFontWeight),
    ),
    textButtonTheme: const TextButtonThemeData(
      style: ButtonStyle(
        textStyle: WidgetStatePropertyAll(TextStyle(fontSize: ThemeTextCst.textMediumSize, fontWeight: ThemeTextCst.textMediumFontWeight)),
        backgroundColor: WidgetStatePropertyAll(ThemeColorCst.textButtonBackgroundColorDark),
        foregroundColor: WidgetStatePropertyAll(ThemeColorCst.textButtonForegroundColorDark),
      ),
    ),
    iconTheme: const IconThemeData(
      color: ThemeColorCst.iconColorDark,
    ),
    extensions: <ThemeExtension<dynamic>>[
      AppThemeColors(
        widgetBackgroundColor: Colors.white,
        appBarLeadingColor: Colors.white,
      ),
    ],
  );

  // 切换主题
  void toggleTheme() {
    isDarkMode.toggle();
    update(); // 这里之所以调update，是因为用到自定义扩展颜色时，对应的widget要用GetBuilder包住，所以要让他生效必须调用update
    Get.changeTheme(currentTheme); // 使用 GetX 全局切换主题
  }
}
